package com.hb.trade.api.expert.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.hb.core.pojo.center.au.dto.OrgDTO;
import com.hb.core.pojo.center.au.dto.RoleDTO;
import com.hb.core.pojo.center.au.dto.UserDTO;
import com.hb.core.pojo.center.au.entity.Role;
import com.hb.core.pojo.expert.entity.ExpertUser;
import com.hb.trade.client.center.OrgClient;
import com.hb.trade.client.expert.ExpertClient;
import lombok.extern.slf4j.Slf4j;
import org.fzzn.core.auth.shiro.service.DefaultUserEntityService;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.user.login.SimpleUserExample;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Shiro登录查询用户的实现类
 *
 * @author ljs
 */
@Slf4j
@Service
public class ExpertUserEntityService extends DefaultUserEntityService {
    protected final ExpertClient expertClient;
    protected final OrgClient orgClient;

    public ExpertUserEntityService(ExpertClient expertClient,
                                   OrgClient orgClient) {
        this.expertClient = expertClient;
        this.orgClient = orgClient;
    }

    @Override
    public SimpleUserExample getByUsername(String username) {
        Message<ExpertUser> userMessage = expertClient.expertUserSearchByUserName(username);
        if (userMessage.isFail()) {
            log.warn(userMessage.getMessage());
            return null;
        }
        ExpertUser user = userMessage.getData();

        // 设置返回信息
        SimpleUserExample userExample = new SimpleUserExample();
        userExample.setUsername(username);
        userExample.setPassword(user.getPassword());
        userExample.setPassword(user.getPassword());
        userExample.setId(user.getId());
        userExample.setOrgId(user.getOrgId());
        userExample.setSaltSign(user.getSaltSource());
        userExample.setFullName(user.getFullName());

        return userExample;
    }

    /**
     * 查询默认拥有代理机构角色
     *
     * @param username
     * @return
     */
    @Override
    public List<String> listRolePermissions(String username) {
        List<String> map = new ArrayList<>();
        map.add("dailijigouceshi");
        log.info("判断当前用户的角色权限: {}", map);
        return map;
    }

    @Override
    public List<String> listFuncPermissions(String username) {
        return null;
    }
}
